package org.jeecg.modules.operations.assistant.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.jeecg.modules.operations.assistant.oa.entity.OaEmployeeInfo;
import org.jeecg.modules.operations.assistant.oa.entity.OaMonthlyAttendance;
import org.jeecg.modules.operations.assistant.oa.entity.OaMonthlyOvertime;
import org.jeecg.modules.operations.assistant.oa.entity.OaOvertimeRecord;
import org.jeecg.modules.operations.assistant.oa.mapper.OaEmployeeInfoMapper;
import org.jeecg.modules.operations.assistant.oa.mapper.OaMonthlyOvertimeMapper;
import org.jeecg.modules.operations.assistant.oa.mapper.OaOvertimeRecordMapper;
import org.jeecg.modules.operations.assistant.oa.service.IOaOvertimeRecordService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 加班记录
 * @Author: jeecg-boot
 * @Date:   2024-12-12
 * @Version: V1.0
 */
@Service
public class OaOvertimeRecordServiceImpl extends ServiceImpl<OaOvertimeRecordMapper, OaOvertimeRecord> implements IOaOvertimeRecordService {
	
	@Autowired
	private OaOvertimeRecordMapper oaOvertimeRecordMapper;

	@Autowired
	private OaEmployeeInfoMapper oaEmployeeInfoMapper;

	@Autowired
	private OaMonthlyOvertimeMapper oaMonthlyOvertimeMapper;

	@Override
	public List<OaOvertimeRecord> selectByMainId(String mainId) {
		return oaOvertimeRecordMapper.selectByMainId(mainId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveOaOvertimeRecord(OaOvertimeRecord entity) {
		Double workingHours = entity.getWorkingHours();
		OaEmployeeInfo oaEmployeeInfo = oaEmployeeInfoMapper.selectById(entity.getEmployeeInfoId());

		if(oaEmployeeInfo.getOvertimeHours()!=null){
			workingHours = oaEmployeeInfo.getOvertimeHours()+workingHours;
		}
		oaEmployeeInfo.setOvertimeHours(workingHours);  // 加班工时
		oaEmployeeInfo.setOvertimeDays(oaEmployeeInfo.getOvertimeHours()/10); // 加班工天
		BigDecimal hourlyWage = oaEmployeeInfo.getHourlyWage().multiply(BigDecimal.valueOf(entity.getWorkingHours()));
		BigDecimal salaryPayable = oaEmployeeInfo.getSalaryPayable().add(hourlyWage);
		oaEmployeeInfo.setSalaryPayable(salaryPayable);
		if(oaEmployeeInfo.getPaidWages()==null){
			oaEmployeeInfo.setNetSalary(salaryPayable);
		}else {
			oaEmployeeInfo.setNetSalary(salaryPayable.subtract(oaEmployeeInfo.getPaidWages()));
		}
		oaEmployeeInfoMapper.updateById(oaEmployeeInfo);
		Date currentDate = entity.getAttendanceTime();
		SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
		SimpleDateFormat monthFormat = new SimpleDateFormat("MM");

		int year = Integer.parseInt(yearFormat.format(currentDate));
		int month = Integer.parseInt(monthFormat.format(currentDate));
		LambdaQueryWrapper<OaMonthlyOvertime> relationQuery = new LambdaQueryWrapper<>();
		relationQuery.eq(OaMonthlyOvertime::getEmployeeInfoId,entity.getEmployeeInfoId());
		relationQuery.eq(OaMonthlyOvertime::getYear,""+year);

		OaMonthlyOvertime oaMonthlyAttendance = oaMonthlyOvertimeMapper.selectOne(relationQuery);
//		return super.save(entity);
		if(oaMonthlyAttendance==null){
			OaMonthlyOvertime oaMonthlyAttendanceNew = new OaMonthlyOvertime();
			oaMonthlyAttendanceNew.setEmployeeInfoId(entity.getEmployeeInfoId());
			oaMonthlyAttendanceNew.setFullName(oaEmployeeInfo.getFullName());
			oaMonthlyAttendanceNew.setYear(""+year);
			if(month==1){
				oaMonthlyAttendanceNew.setJanuary(entity.getWorkingHours());
			} else if(month==2){
				oaMonthlyAttendanceNew.setFebruary(entity.getWorkingHours());
			} else if(month==3){
				oaMonthlyAttendanceNew.setMarch(entity.getWorkingHours());
			} else if(month==4){
				oaMonthlyAttendanceNew.setApril(entity.getWorkingHours());
			} else if(month==5){
				oaMonthlyAttendanceNew.setMay(entity.getWorkingHours());
			} else if(month==6){
				oaMonthlyAttendanceNew.setJune(entity.getWorkingHours());
			} else if(month==7){
				oaMonthlyAttendanceNew.setJuly(entity.getWorkingHours());
			} else if(month==8){
				oaMonthlyAttendanceNew.setAugust(entity.getWorkingHours());
			} else if(month==9){
				oaMonthlyAttendanceNew.setSeptember(entity.getWorkingHours());
			} else if(month==10){
				oaMonthlyAttendanceNew.setOctober(entity.getWorkingHours());
			} else if(month==11){
				oaMonthlyAttendanceNew.setNovember(entity.getWorkingHours());
			} else if(month==12){
				oaMonthlyAttendanceNew.setDecember(entity.getWorkingHours());
			}
			oaMonthlyOvertimeMapper.insert(oaMonthlyAttendanceNew);
		}else {
			if(month==1){
				if(oaMonthlyAttendance.getJanuary()==null) {
					oaMonthlyAttendance.setJanuary(entity.getWorkingHours());
				}else{
					oaMonthlyAttendance.setJanuary(oaMonthlyAttendance.getJanuary()+entity.getWorkingHours());
				}
			} else if(month==2){
				if(oaMonthlyAttendance.getFebruary()==null){
					oaMonthlyAttendance.setFebruary(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setFebruary(oaMonthlyAttendance.getFebruary()+entity.getWorkingHours());
				}
			} else if(month==3){
				if(oaMonthlyAttendance.getMarch()==null){
					oaMonthlyAttendance.setMarch(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setMarch(oaMonthlyAttendance.getMarch()+entity.getWorkingHours());
				}
			} else if(month==4){
				if(oaMonthlyAttendance.getApril()==null){
					oaMonthlyAttendance.setApril(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setApril(oaMonthlyAttendance.getApril()+entity.getWorkingHours());
				}
			} else if(month==5){
				if(oaMonthlyAttendance.getMay()==null){
					oaMonthlyAttendance.setMay(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setMay(oaMonthlyAttendance.getMay()+entity.getWorkingHours());
				}
			} else if(month==6){
				if(oaMonthlyAttendance.getJune()==null){
					oaMonthlyAttendance.setJune(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setJune(oaMonthlyAttendance.getJune()+entity.getWorkingHours());
				}
			} else if(month==7){
				if(oaMonthlyAttendance.getJuly()==null){
					oaMonthlyAttendance.setJuly(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setJuly(oaMonthlyAttendance.getJuly()+entity.getWorkingHours());
				}
			} else if(month==8) {
				if (oaMonthlyAttendance.getAugust()==null) {
					oaMonthlyAttendance.setAugust(entity.getWorkingHours());
				} else{
					oaMonthlyAttendance.setAugust(oaMonthlyAttendance.getAugust()+entity.getWorkingHours());
				}
			} else if(month==9){
				if(oaMonthlyAttendance.getSeptember()==null){
					oaMonthlyAttendance.setSeptember(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setSeptember(oaMonthlyAttendance.getSeptember()+entity.getWorkingHours());
				}
			} else if(month==10){
				if(oaMonthlyAttendance.getOctober()==null){
					oaMonthlyAttendance.setOctober(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setOctober(oaMonthlyAttendance.getOctober()+entity.getWorkingHours());
				}
			} else if(month==11){
				if(oaMonthlyAttendance.getNovember()==null){
					oaMonthlyAttendance.setNovember(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setNovember(oaMonthlyAttendance.getNovember()+entity.getWorkingHours());
				}
			} else if(month==12){
				if(oaMonthlyAttendance.getDecember()==null){
					oaMonthlyAttendance.setDecember(entity.getWorkingHours());
				}else {
					oaMonthlyAttendance.setDecember(oaMonthlyAttendance.getDecember()+entity.getWorkingHours());
				}
			}
			oaMonthlyOvertimeMapper.updateById(oaMonthlyAttendance);
		}
		entity.setFullName(oaEmployeeInfo.getFullName());
		return SqlHelper.retBool(this.getBaseMapper().insert(entity));
	}
}
